// import * as types from '@/store/mutation-type.js'
import * as CONSTANTS from '@/constants/index'
import REQUEST from '@/api/index.js'
import ROUTER from '@/router/index'
// import STORE from '@/store/index'
import { API_BASEPREFIX } from '@/constants/index'
import { Toast } from 'vant'

export default {
  /**
   * 根据用户ID，获取对应宝宝ID的数据
   */
  [CONSTANTS.DISPATCH_GETASYNC_USERALLBABIES]: async ({ state, commit }, payload) => {
    try {
      // console.warn('actions/user - DISPATCH_GETASYNC_USERALLBABIES:')
      // console.log('start => ')
      const { userId } = payload;
      if (!userId) return Toast.fail('LACK OF USERID');
      const params = {userId}
      const { data } = await REQUEST[CONSTANTS.API_GET_USERALLBABIES](params)
      if (!data.success) throw new Error(data.info)
      const resData = data.data

      commit(CONSTANTS.COMMIT_UPDATE_USER_CURRENTUSERDATA, resData)

      //更新当前宝宝数据
      const { user } = state;
      const { currentBabyId } = user;
      const item = resData.find(i => i.id * 1 === currentBabyId * 1);
      
      //没有头像的情况下，使用默认头像
      if (!item.head) item.head = item.sex * 1 === 1 ? require("./../../assets/images/user/male.png") : require("./../../assets/images/user/female.png");
      
      commit(CONSTANTS.COMMIT_UPDATE_USER_CURRENTBABYDATA, item)
      commit(CONSTANTS.COMMIT_STORAGE_VUEXSTATE)
      
    } catch (err) {
      console.log(err)
      Toast.fail(err.message)
    }
  },
}
